// 定义 Vehicle 类
class Vehicle {
    public brand: string;
    public year: number;
    private static vehicleCount: number = 0;

    constructor(brand: string, year: number) {
        this.brand = brand;
        this.year = year;
        this.validateYear();
        Vehicle.incrementVehicleCount();
    }

    private validateYear(): void {
        if (this.year > new Date().getFullYear()) {
            throw new Error("Year cannot be in the future.");
        }
    }

    public getInfo(): string {
        return `Brand: ${this.brand}, Year: ${this.year}`;
    }

    public static incrementVehicleCount(): void {
        Vehicle.vehicleCount++;
    }

    public static getVehicleCount(): number {
        return Vehicle.vehicleCount;
    }
}

// 定义 Car 类
class Car extends Vehicle {
    private _mileage: number;

    constructor(brand: string, year: number, mileage: number) {
        super(brand, year);
        this._mileage = mileage;
    }

    public get mileage(): number {
        return this._mileage;
    }

    public set mileage(value: number) {
        if (value < 0) {
            throw new Error("Mileage cannot be negative.");
        }
        this._mileage = value;
    }

    public drive(miles: number): void {
        if (miles < 0) {
            throw new Error("Miles driven cannot be negative.");
        }
        this._mileage += miles;
    }

    public getInfo(): string {
        return `${super.getInfo()}, Mileage: ${this._mileage}`;
    }

    public static compareMileage(car1: Car, car2: Car): Car {
        return car1.mileage > car2.mileage ? car1 : car2;
    }
}

//调用
const car1 = new Car("Toyota", 2020, 15000);
const car2 = new Car("Ford", 2021, 20000);

car2.drive(500);


const higherMileageCar = Car.compareMileage(car1, car2);
console.log(higherMileageCar.getInfo());


Vehicle.incrementVehicleCount();
console.log(`Current vehicle count: ${Vehicle.getVehicleCount()}`);
